from django.shortcuts import render
from useropera.models import Order, Favorite,ServerCenter
from useropera.serializers import (
    OrderSerializer,
    FavoriteSerializer_GET,
    FavoriteSerializer_POST,
    AddOrder,
    ServerCenterSerializer_Get,
    ServerCenterSerializer_Post
)
from rest_framework import viewsets, mixins, permissions
import random
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

# Create your views here.

# 用户订单创建及订单详情
class OrderViewset(viewsets.ModelViewSet):
    """
        Order CRUD
    """

    permission_classes = [permissions.IsAuthenticated]
    authentication_classes = [JSONWebTokenAuthentication]
    # serializer_class = OrderSerializer

    def get_queryset(self):  #  单个用户可以有多个地址，所以使用get_queryset()而不是get_object()
        user = self.request.user
        queryset = Order.objects.filter(owner=user)
        return queryset

    def perform_create(self, serializer):
        nums = self.createordernumber()
        username = str(self.request.user.id)
        print(username)
        ordernum = nums + "HW" + username
        serializer.save(owner=self.request.user, status="0", ordernum=ordernum)

    def createordernumber(self):
        seeds = "1234567890"
        orderNumStr = []
        for i in range(6):
            orderNumStr.append(random.choice(seeds))

        return "".join(orderNumStr)

    def get_serializer_class(self):  ## post的action对应着create！！！！！
        if self.action == "create" or self.action == "update":
            serializer_class = AddOrder
        else:
            serializer_class = OrderSerializer
        return serializer_class


# 用户收藏
class FavoriteViewset(viewsets.ModelViewSet):
    permission_classes = [permissions.IsAuthenticated]
    authentication_classes = [JSONWebTokenAuthentication]

    def get_queryset(self):
        user = self.request.user
        queryset = Favorite.objects.filter(owner=user)
        return queryset

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

    def get_serializer_class(self):
        if self.action == "list":
            serializer_class = FavoriteSerializer_GET
        else:
            serializer_class = FavoriteSerializer_POST
        return serializer_class

    


# 客服中心
class ServerCenterViewset(viewsets.ModelViewSet):
    # serializer_class = ServerCenterSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    # permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        isall = self.request.query_params.get("isall",None)
        owners = self.request.user
        if isall is not None:
            queryset = ServerCenter.objects.filter(owner=owners)
        else:
            queryset = ServerCenter.objects.all()
        return queryset
    
    def createordernumber(self):
        seeds = "1234567890"
        orderNumStr = []
        for i in range(6):
            orderNumStr.append(random.choice(seeds))

        return "".join(orderNumStr)

    def perform_create(self, serializer):
        servernums = self.createordernumber()
        srevernumsw = "SH2020" + servernums
        serializer.save(owner=self.request.user,servernumber=srevernumsw)

    def get_serializer_class(self):
        if self.action == "create":
            serializer_class = ServerCenterSerializer_Post
        elif self.action == "update":
            serializer_class = ServerCenterSerializer_Post
        else:
            serializer_class = ServerCenterSerializer_Get
        return serializer_class